package com.huanletao.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

/**
 * <p>
 * AnnotationUtils 注解工具类
 * </p>
 * package: com.huanletao.annotation
 * description:
 * className: AnnotationUtils
 * author:  踏雪无痕
 * copyright: Copyright (c) 2022/8/24 14:32
 * since: 1.0.0
 */
public abstract class AnnotationUtils {

    /**
     * 查找一个 注解；
     * @param annotatedElement
     * @param annotationType
     * @param <A>
     * @return
     */
    public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
        A annotation = annotatedElement.getAnnotation(annotationType);
        if (annotation == null) {
            Annotation[] annotations = annotatedElement.getAnnotations();
            for (Annotation annotationItem : annotations) {
                annotation = annotationItem.annotationType().getAnnotation(annotationType);
                if (annotation != null) {
                    break;
                }
            }
        }
        return annotation;
    }

    /**
     *
     * @param annotatedElement
     * @param annotationType
     * @param <A>
     * @return
     */
    public static<A extends Annotation> Boolean hasAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType){
        return annotatedElement.isAnnotationPresent(annotationType);
    }
}
